<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      size="small"
      label-width="100px"
    >
      <div style="white-space: nowrap">
        <el-form-item label="区　　域">
          <treeselect
            style="width: 200px"
            v-model="queryParams.pid"
            :options="optionsList"
            :normalizer="normalizer"
            placeholder="选择区域"
            class="search-tree"
          />
        </el-form-item>
        <el-form-item label="文档名称">
          <el-input
            style="width: 200px"
            v-model="queryParams.doc"
            placeholder="请输入文档名称"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="关键词">
          <el-input
            style="width: 200px"
            v-model="queryParams.keyword"
            placeholder="请输入关键词"
            clearable
            size="small"
          />
        </el-form-item>
      </div>
      <div style="white-space: nowrap">
        <el-form-item label="开始时间">
          <el-date-picker
            style="width: 200px"
            v-model="queryParams.starttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            style="width: 200px"
            v-model="queryParams.endtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="cyan"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </div>
    </el-form>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column label="所属区域" align="left" prop="path">
        <template slot-scope="scope">
          <span class="path" @click="openInfoPop(scope.row)">{{
            scope.row.path
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="文档名称"
        align="left"
        prop="doc"
      ></el-table-column>
      <el-table-column
        label="打印机名称"
        align="left"
        prop="printer"
      ></el-table-column>
      <el-table-column
        label="关键词"
        align="left"
        prop="keyword"
        width="80"
      ></el-table-column>
      <el-table-column
        label="页数"
        align="center"
        prop="pages"
        width="50"
      ></el-table-column>
      <el-table-column
        label="份数"
        align="center"
        prop="copies"
        width="50"
      ></el-table-column>
      <el-table-column
        label="纸张"
        align="center"
        prop="paper"
        width="120"
      ></el-table-column>
      <el-table-column
        label="打印时间"
        align="center"
        prop="time"
        width="200"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.userId !== 1"
            size="mini"
            type="text"
            icon="el-icon-picture"
            @click="openPic(scope.row)"
            >内容</el-button
          >
          <el-button
            v-if="scope.row.userId !== 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="pagination"
    />
    <PicPop ref="PicPop"></PicPop>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getDataTree } from "@/api/system/dept";
import { getAlarmList, deleteAlarm } from "@/api/alarm.js";
import PortraitTool from "@/utils/portrait-tool";
import { getMerchantById } from "@/api/system/printingShop";

export default {
  name: "Computer",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: false,

      tableList: [],
      queryParams: {
        doc: "",
        keyword: "",
        starttime: "",
        endtime: "",
        pageNum: 1,
        pageSize: 20,
        pid: null,
      },
      optionsList: [],
      total: 0, //总条数
    };
  },
  created() {},

  methods: {
    getAlarmList() {
      getAlarmList(this.queryParams).then((res) => {
        if (res.code == "200" && res.data.datas) {
          this.tableList = res.data.datas;
          this.total = res.data.TotalNum;
        }
      });
    },
    pagination(page) {
      this.queryParams.pageNum = page.page;
      this.queryParams.pageSize = page.limit;
      this.getAlarmList();
    },
    openPic(row) {
      this.$refs.PicPop.openPop(row);
    },
    openInfoPop(row) {
      this.getMerchantById({ id: row.pid });
    },
    getMerchantById(params) {
      getMerchantById(params).then((res) => {
        if (res.code === "200") {
          this.$refs.AddShopInfoPop.openPop("edit", res.data.datas[0]);
        }
      });
    },
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.doc + '"的打印记录?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteAlarm({ id: row.id });
        })
        .then(() => {
          this.getAlarmList();
          this.msgSuccess("删除成功");
        });
    },
    /** 查询部门树结构 */
    getDeptTreeselect() {
      getDataTree().then((response) => {
        this.optionsList = PortraitTool.toTreeData(
          response.data.datas.filter((i) => {
            return i.id != "0000000000000000";
          })
        );
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getAlarmList();
    },
    resetQuery() {
      this.queryParams = {};
    },
  },
  mounted() {
    this.getDeptTreeselect();
    if (this.$route.query.id) {
      this.queryParams.id = this.$route.query.id;
    }
    this.getAlarmList();
  },
};
</script>
<style scoped>
.path {
  color: #004080;
  cursor: pointer;
}
</style>
